<template>
	<view>
		<view class="header">
			<view class="navbar">
				<view class="navbar-fixed">
					<!-- 状态栏小程序d撑起高度 -->
					<view :style="{ height: statusBarHeight + 'px' }"></view>
					<view class="navbar-content" :style="{ height: navBarHeight + 'px', width: windowWidth + 'px' }">
						<view class="navbar-search">首页</view>
					</view>
				</view>
				<view :style="{ height: statusBarHeight + navBarHeight + 'px' }"></view>
			</view>
			<view class="searchbox">
				<view class="imgbox" @click="toaddress()">
					<view class="placebox">
						<view>{{ addressinfo.name || "定位" }}</view>
						<image src="../../static/sanjiao.png"></image>
					</view>
					<view>选择小区</view>
				</view>
				<view class="inputbox" @click="tosearch()">
					<image src="../../static/ss.png"></image>
					<input disabled placeholder="搜索商品" v-model="kewword" placeholder-style="color:#888888" />
				</view>
				<view class="searchsure" @click="tocar()">
					<image src="../../static/car.png"></image>
				</view>
			</view>
			<view class="uni-margin-wrap">
				<swiper class="swiper" circular :indicator-dots="true" :autoplay="true" :interval="2000" :duration="500">
					<swiper-item class="swiper-item" v-for="(item, index) in banner" :key="index" @click="tourl(item)">
						<image :src="item.img"></image>
					</swiper-item>
				</swiper>
			</view>
			<view class="catebox">
				<view class="cateitem" v-for="(item, index) in list" :key="index" @click="topage(item, index)">
					<image :src="item.img"></image>
					<view style="margin-top: 20rpx">{{ item.tit }}</view>
				</view>
			</view>
		</view>
		<view class="goodsname" v-if="jx_course.length">
			<view class="titlebox">
				<view class="tit">精选课程</view>
				<view class="english"> · COURSE </view>
			</view>
			<view class="morebox" @click="more(1)">
				<view>更多</view>
				<image src="../../static/jt.png"></image>
			</view>
		</view>
		<goods :list="jx_course" @togoodsinfo="togoodsinfo"></goods>
		<view class="goodsname" v-if="teacher.length">
			<view class="titlebox">
				<view class="tit">教师风采</view>
				<view class="english"> · TEACHER </view>
			</view>
			<view class="morebox" @click="more(2)">
				<view>更多</view>
				<image src="../../static/jt.png"></image>
			</view>
		</view>
		<teacher :list="teacher"></teacher>
		<view class="goodsname">
			<view class="titlebox">
				<view class="tit">好课推荐</view>
				<view class="english" style="width: 300rpx"> · RECOMMEND </view>
			</view>
			<view class="morebox" @click="more(3)">
				<view>更多</view>
				<image src="../../static/jt.png"></image>
			</view>
		</view>
		<classes :list="tj_course"></classes>
		<tarbar :actbar="actbar"></tarbar>
	</view>
</template>

<script>
import goods from "../../components/goods.vue"
import teacher from "../../components/teacher.vue"
import classes from "../../components/goodsclass.vue"
import tarbar from "../../components/tabbar.vue"
export default {
	components: {
		goods,
		teacher,
		classes,
		tarbar,
	},
	data() {
		return {
			statusBarHeight: 20,
			/* 状态栏高度 */
			navBarHeight: 45,
			/* 导航栏高度 */
			windowWidth: 375,
			/* 窗口宽度 */
			/* 设定状态栏默认高度 */
			list: [
				{
					tit: "精选课程",
					img: "../../static/jxkc.png",
					path: "/pages/goodscourses/goodscourses",
					id: 0,
				},
				{
					tit: "教师风采",
					img: "../../static/jsfc.png",
					path: "/pages/teacherdemeanor/teacherdemeanor",
					id: 1,
				},
				{
					tit: "社区活动",
					img: "../../static/sqhd.png",
					path: "/pages/Comactivities/Comactivities",
					id: 2,
				},
				{
					tit: "社区优选",
					img: "../../static/sczx.png",
					path: "/pages/shop/shop",
					id: 3,
				},
				{
					tit: "新闻中心",
					img: "../../static/xwzx.png",
					path: "/pagemy/news/news",
					id: 4,
				},
				// {
				// 	tit: '成为志愿者',
				// 	img: '../../static/cwzyz.png',
				// 	path:'/pages/authoration/authoration',
				// 	id:5
				// },
				// {
				// 	tit:'社区介绍',
				// 	img: '../../static/shequ.png',
				// 	path:'/pagemy/shequ/shequ',
				// 	id:6
				// }
			],
			page_index: 1,
			is_data: true,
			actbar: 0,
			banner: [], //轮播图
			jx_course: [], //精选
			teacher: [], //老师列表
			tj_course: [], //好课推荐
			addressinfo: uni.getStorageSync("address") || null,
			kewword: "",
			token: uni.getStorageSync("token") || "",
			myinfo: {},
			changeStr: "",
			latitude: "",
			longitude: "",
		}
	},
	onLoad({ scene }) {
		if (scene) {
			uni.setStorageSync("pid", scene)
		}
	},
	async onShow() {
		this.addressinfo = uni.getStorageSync("address")
		if (!this.addressinfo) {
			// await this.getplace()
		}
		this.getnav()
		this.getlist()
		this.token = uni.getStorageSync("token") || ""
		if (this.token) {
			this.getmyinfo()
		}
	},
	methods: {
		getnav() {
			// 获取手机系统信息
			const info = uni.getSystemInfoSync()
			// 设置状态栏高度（H5顶部无状态栏小程序有状态栏需要撑起高度）
			this.statusBarHeight = info.statusBarHeight
			this.windowWidth = info.windowWidth
			// 除了h5 app mp-alipay的情况下执行
			// #ifndef H5 || APP-PLUS || MP-ALIPAY
			// 获取胶囊的位置
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			console.log(menuButtonInfo)
			// (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏内的高度) = 导航栏的高度
			this.navBarHeight = menuButtonInfo.bottom - info.statusBarHeight + (menuButtonInfo.top - info.statusBarHeight)
			this.windowWidth = menuButtonInfo.left
			// #endif
		},
		getlist() {
			this.$http({
				url: "/api/Activity/home",
				data: {
					latitude: this.latitude || "",
					longitude: this.longitude || "",
					community_id: this.addressinfo.id || "",
				},
			}).then((res) => {
				this.banner = res.data.banner
				this.jx_course = res.data.jx_course
				this.teacher = res.data.teacher
				this.tj_course = res.data.tj_course
			})
		},
		getmyinfo() {
			this.$http({
				url: "/api/Course/user_info",
			}).then((res) => {
				this.myinfo = res.data
				//identity 1普通用户2志愿者3老师  老师 is_teacher 1是0否
			})
		},
		topage(item, index) {
			// if(this.myinfo.is_teacher==1&&index==5){
			// 	uni.showToast({
			// 		title:'您已是教师',
			// 		icon:'none'
			// 	})
			// 	return
			// }

			if (item.id == 5 && this.myinfo.is_post_volun == 1) {
				//is_post_volun 0未提交 1已提交
				if (this.myinfo.identity == 2) {
					uni.showToast({
						title: "您已是志愿者",
						icon: "none",
					})
					return
				} else {
					uni.showToast({
						title: "后台审核中，请耐心等待",
						icon: "none",
					})
					return
				}
			} else {
				uni.navigateTo({
					url: item.path,
				})
			}
		},
		togoodsinfo(result) {
			const info = JSON.parse(JSON.stringify(result))
			uni.navigateTo({
				url: "/pages/coursesinfo/coursesinfo?id=" + info.id,
			})
		},
		getplace() {
			return new Promise((resolve, reject) => {
				let that = this
				uni.getLocation({
					type: "wgs84",
					success: async (res) => {
						this.latitude = res.latitude
						this.longitude = res.longitude
						await that.getcommunity()
						resolve()
					},
				})
			})
		},
		getcommunity() {
			this.$http({
				url: "/api/Activity/community_list",
				data: {
					lat: this.latitude,
					long: this.longitude,
				},
			}).then((res) => {
				if (!this.addressinfo) {
					this.addressinfo = res.data[0]
				}
			})
		},
		more(n) {
			if (n == 1) {
				uni.navigateTo({
					url: "/pages/goodscourses/goodscourses",
				})
			}
			if (n == 2) {
				uni.navigateTo({
					url: "/pages/teacherdemeanor/teacherdemeanor",
				})
			}
			if (n == 3) {
				uni.navigateTo({
					url: "/pages/classrecommendation/classrecommendation",
				})
			}
		},
		toaddress() {
			uni.navigateTo({
				url: "/pages/selectcommunity/selectcommunity",
			})
		},
		tosearch() {
			uni.navigateTo({
				url: "/pages/search/search",
			})
		},
		tocar() {
			uni.showToast({
				icon: "none",
				title: "【敬请期待】",
			})
			return
			uni.navigateTo({
				url: "/pageshop/shoppingCart/shoppingCart",
			})
		},
		tourl(item) {
			console.log(item.url)
			// console.log(item.url)
			if (item.url) {
				uni.navigateTo({
					url: item.url,
				})
			}
		},
		onShareAppMessage(res) {
			if (res.from === "button") {
				// 来自页面内分享按钮
				console.log(res.target)
			}
			// return {
			//   title: "邀请好友",
			//   path: '/pages/index/index?pid='+this.id
			// }
		},
		onShareTimeline() {
			return {
				title: "快乐生活，就到众享幸福家",
				path: "/pages/index/index",
			}
		},
	},
}
</script>

<style>
page {
	background-color: #f7f7f7;
}
</style>
<style lang="scss" scoped>
.header {
	// height: 1062rpx;
	height: 795rpx;
	width: 100%;
	background-size: 100% 1062rpx;
	background: linear-gradient(to bottom, #f9c6cc, #f5f5f5);
}

.navbar {
	.navbar-fixed {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 399;
		width: 100%;
		// height: 102rpx;
		// padding-bottom: 20rpx;
		background: transparent;

		.navbar-content {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 0rpx 30rpx;
			// height: 142rpx;
			box-sizing: border-box;

			image {
				width: 19rpx;
				height: 35rpx;
				margin-right: 20rpx;
			}

			.navbar-search {
				display: flex;
				align-items: center;
				// padding: 0 10px;
				font-size: 36rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #222;
				position: absolute;
				left: 50%;
				transform: translate(-50%, 0);
			}
		}
	}
}

.searchbox {
	width: 690rpx;
	height: 70rpx;
	// opacity: 0.12;
	// background: rgba(0, 0, 0, 0.12);
	border-radius: 36rpx;
	margin: 30rpx auto 0 auto;
	display: flex;
	align-items: center;
	justify-content: space-between;

	.imgbox {
		text-align: center;
		font-size: 24rpx;
		font-family: Source Han Sans CN-Regular, Source Han Sans CN;
		font-weight: 400;
		color: #555555;

		.placebox {
			display: flex;
			font-size: 32rpx;
			font-family: Source Han Sans CN-Medium, Source Han Sans CN;
			font-weight: 500;
			color: #222222;

			image {
				width: 16rpx;
				height: 8rpx;
				margin-top: 20rpx;
				margin-left: 18rpx;
			}

			> view {
				width: 65rpx;
				-webkit-line-clamp: 1;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}

	.inputbox {
		padding: 0 0 0 20rpx;
		width: 468rpx;
		height: 70rpx;
		background: #ffffff;
		border-radius: 36rpx;
		display: flex;
		align-items: center;

		> input {
			height: 70rpx;
		}

		image {
			width: 33rpx;
			height: 28rpx;
			margin-right: 20rpx;
		}
	}

	.searchsure {
		// width: 95rpx;
		image {
			width: 45rpx;
			height: 46rpx;
		}
	}
}

.catebox {
	display: flex;
	align-items: center;
	flex-wrap: wrap;
	width: 690rpx;
	margin: 50rpx auto 0 auto;
	margin-top: 40rpx;
	background-color: white;
	border-radius: 20rpx;
	padding: 20rpx;
	box-sizing: border-box;

	.cateitem {
		width: 20%;
		text-align: center;
		font-size: 28rpx;
		font-family: Source Han Sans CN-Regular, Source Han Sans CN;
		font-weight: 400;
		color: #222222;
		margin-top: 10rpx;

		image {
			width: 94rpx;
			height: 94rpx;
		}
	}
}

.uni-margin-wrap {
	width: 690rpx;
	margin: 22rpx auto 0 auto;
}

.swiper {
	height: 250rpx;
}

.swiper-item {
	display: block;
	height: 250rpx;
	line-height: 250rpx;
	text-align: center;

	image {
		width: 690rpx;
		height: 250rpx;
		border-radius: 16rpx;
	}
}

.listbox {
	width: 750rpx;
	background: #ffffff;
	border-radius: 36rpx;
	margin-top: -35rpx;
	padding: 35rpx 0;
}

.goodsname {
	width: 690rpx;
	margin: 0 auto;
	height: 108rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;

	.titlebox {
		display: flex;
		align-items: center;

		.tit {
			width: 180rpx;
			font-size: 40rpx;
			font-family: SourceHanSansCN-Medium-, SourceHanSansCN-Medium;
			font-weight: normal;
			color: #222222;
		}

		.english {
			width: 202rpx;
			height: 56rpx;
			background: url($uni-url + "/bg1.png") no-repeat;
			background-size: 152rpx 56rpx;
			font-size: 32rpx;
			font-family: Source Han Sans CN-Bold, Source Han Sans CN;
			font-weight: bold;
			color: #ff8ea5;
			line-height: 56rpx;
		}
	}

	.morebox {
		display: flex;
		width: 122rpx;
		height: 54rpx;
		background: #ffffff;
		border-radius: 32rpx;
		align-items: center;
		font-size: 26rpx;
		font-family: Source Han Sans CN-Regular, Source Han Sans CN;
		font-weight: 400;
		color: #555555;
		justify-content: center;

		image {
			width: 22rpx;
			height: 12rpx;
			margin-left: 8rpx;
		}
	}
}
</style>
